home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5837 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I round and truncate floats to integers?
  5. Date: 21 Feb 1996 01:14:00 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb20181400@qcd.lanl.gov>
  8. References: <4g009b$c2n@news.tuwien.ac.at> <harmon.824447677@pegasus.montclair.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: harmon@pegasus.montclair.edu's message of 16 Feb 1996 00:38:09 -0500
  13.  
  14. In article <harmon.824447677@pegasus.montclair.edu>
  15. harmon@pegasus.montclair.edu (Derek Harmon) writes: 
  16. <snip>
  17.    >          If typecasting is supposed to do the trick, then how do I
  18.    >distinguish cases 1) and 2)?
  19.  
  20.       It is important to note that a C typecast only tells the compiler to treat
  21.    some form of data as another form of data.  No change is made to the data.
  22.    The binary representation of a double floating point value is very different
  23.    from that of an integer, and just telling the compiler to treat it as an int
  24.    will not solve your problem (for example, 7 != (int)'7').
  25.  
  26. If you are not proficient in C, please test your statements before
  27. posting answers. Nobody has the right to air their confusion in a
  28. forum which is likely to mislead other people. A cast in C can indeed
  29. change the bit pattern that represents a value. 
  30.  
  31. In particular, find a compiler that gives (int)7.5 != 7 or (int)7.4 !=
  32. 7. Do you think that the bit representation of the double 7.5 is the
  33. same as that of the int 7? Or do you think that 7.5 and 7.4 have the
  34. same bit string?
  35.  
  36. (int)'7' is a bit ridiculous. '7' is an int, converting it to int just
  37. gives back the same int viz. '7'. The difference here is that the int
  38. has been represented as a character literal, and the character literal
  39. '7' is different from the decimal constant 7. It is like saying that
  40. the octal constant 023 is not the same as the decimal constant 23: it
  41. certainly isn't: in fact (int)023 != 23, but that has little to do
  42. with the cast.
  43.     
  44. Cheers
  45. Tanmoy
  46. --
  47. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  48. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  49. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  50. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  51. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  52. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  53.